package LC.hash;

class LC202 {

    public int getNext(int n) {
        int totalSum = 0;
        while (n > 0) {
            int d = n % 10;
            n = n / 10;
            totalSum += d * d;
        }
        return totalSum;
    }

    public boolean isHappy(int n) {
        int slowRunner = n;
        int fastRunner = getNext(n);
//如果给定的数字最后会一直循环重复，那么快的指针（值）一定会追上慢的指针（值），
//也就是两者一定会相等。如果没有循环重复，那么最后快慢指针也会相等，且都等于1。
        while (fastRunner != 1 && slowRunner != fastRunner) {
            slowRunner = getNext(slowRunner);
            fastRunner = getNext(getNext(fastRunner));
        }
        return fastRunner == 1;
    }
}
